package com.blb.community.controller.zy;


import com.blb.community.pojo.ResponseResult;
import com.blb.community.pojo.ZyCommunity;
import com.blb.community.pojo.zy.ZyVisitor;
import com.blb.community.service.IZyCommunityService;
import com.blb.community.service.ZyOwnerService;
import com.blb.community.service.zy.ZyVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 访客管理控制层
 */
@CrossOrigin
@RestController
@RequestMapping("/visitor/*")
public class ZyVisitorController {

    @Autowired
    private ZyOwnerService zyOwnerService;
    @Autowired
    private ZyVisitorService zyVisitorService;
    @Resource
    private IZyCommunityService zyCommunityService;


    /**
     * 根据业主id查询访客列表
     *
     * @param createById
     * @return
     */
    @GetMapping("visitors/{createById}")
    public ResponseResult findVisitorByCreateById(@PathVariable Long createById) {
        List<ZyVisitor> zyVisitorList = zyVisitorService.getByCreateById(createById);
        return ResponseResult.ok(zyVisitorList);
    }

    /**
     * 查询小区列表
     *
     * @return
     */
    @GetMapping("communities")
    private ResponseResult getCommunityList() {
        List<ZyCommunity> list = zyCommunityService.list();
        return ResponseResult.ok(list);
    }


    /**
     * 添加访客
     *
     * @param zyVisitor
     * @return
     */
    @PostMapping("visitor")
    public ResponseResult insertVisitor(@RequestBody ZyVisitor zyVisitor) {
        zyVisitor.setCreateTime(LocalDateTime.now());
        zyVisitorService.save(zyVisitor);
        return ResponseResult.ok(200);
    }


    /**
     * 访客信息删除功能
     *
     * @param visitorId
     * @return
     */
    @DeleteMapping("visit/{visitorId}")
    public ResponseResult deleteVisitor(@PathVariable Long visitorId) {
        ZyVisitor zyVisitor = zyVisitorService.getById(visitorId);
        zyVisitor.setDelFlag(1);
        zyVisitorService.updateById(zyVisitor);
        return ResponseResult.ok(200);
    }

}
